﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DalGen.Contracts;
using DalGen.Entity;
using WeifenLuo.WinFormsUI.Docking;
using DalGen.Util;

namespace DalGen
{
    public partial class ProjectSet : DockContent
    {
        EProjectInfo info;

        public ProjectSet(EProjectInfo projectInfo)
        {
            info = projectInfo;
            InitializeComponent();
            GetAttr();

        }


        #region 读写属性

        private void GetAttr()
        {
            txtTitle.Text = info.Title;
            txtVersion.Text = info.Version;
            txtCompany.Text = info.Company;
            txtCopyRight.Text = info.CopyRight;
            txtDesigner.Text = info.Designer;
            txtEmail.Text = info.Email;
            txtProjectNameSpace.Text = info.ProjectNameSpace;
            txtProjectFolder.Text = info.ProjectSavePath;
            txtEntityNameSpace.Text = info.EntityNameSpace;
            txtEntityFolder.Text = info.EntitySavePath;
            txtMapFolder.Text = info.MapXmlSavePath;
            txtIRespositoryNameSpace.Text = info.IRepositoryNameSpace;
            txtIRespositoryFolder.Text = info.IRepositorySavePath;
            txtRespositoryNameSpace.Text = info.RepositoryNameSpace;
            txtRespositoryFolder.Text = info.RepositorySavePath;
            txtIServiceNameSpace.Text = info.IServiceNameSpace;
            txtIServiceFolder.Text = info.IServiceSavePath;
            txtServiceNameSpace.Text = info.ServiceNameSpace;
            txtServiceFolder.Text = info.ServiceSavePath;
            txtServiceModelNameSpace.Text = info.ServiceModelNameSpace;
            txtServiceModelFolder.Text = info.ServiceModelSavePath;


        }

        private void SetAttr()
        {

            info.Email = txtEmail.Text;
            info.Company = txtCompany.Text;
            info.CopyRight = txtCopyRight.Text;
            info.Designer = txtDesigner.Text;
            info.Title = txtTitle.Text;
            info.Version = txtVersion.Text;

            info.ProjectSavePath = txtProjectFolder.Text;
            info.ProjectNameSpace = txtProjectNameSpace.Text;

            info.EntityNameSpace = txtEntityNameSpace.Text;
            info.EntitySavePath = txtEntityFolder.Text;
            info.MapXmlSavePath = txtMapFolder.Text;

            info.IRepositoryNameSpace = txtIRespositoryNameSpace.Text;
            info.IRepositorySavePath = txtIRespositoryFolder.Text;

            info.RepositoryNameSpace = txtRespositoryNameSpace.Text;
            info.RepositorySavePath = txtRespositoryFolder.Text;

            info.IServiceNameSpace = txtIServiceNameSpace.Text;
            info.IServiceSavePath = txtIServiceFolder.Text;

            info.ServiceNameSpace = txtServiceNameSpace.Text;
            info.ServiceSavePath = txtServiceFolder.Text;

            info.ServiceModelNameSpace = txtServiceModelNameSpace.Text;
            info.ServiceModelSavePath = txtServiceModelFolder.Text;


        }

        #endregion

        #region 保存路径

        private void btnProjectFolder_Click(object sender, EventArgs e)
        {
            SetSavePath(txtProjectFolder);
        }

        private void btnEntityBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtEntityFolder);
        }

        private void btnMapBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtMapFolder);
        }

        private void btnIRespositoryBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtIRespositoryFolder);
        }

        private void btnRespositoryBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtRespositoryFolder);
        }

        private void btnIServiceBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtIServiceFolder);
        }

        private void btnServiceBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtServiceFolder);
        }

        private void SetSavePath(TextBox textBox)
        {
            folderBrowser.Description = "请选择源代码保存路径";
            folderBrowser.SelectedPath = textBox.Text;
            if (folderBrowser.ShowDialog(this) == DialogResult.OK)
            {
                string sSelectPath = folderBrowser.SelectedPath;
                textBox.Text = sSelectPath;
                if (textBox.Name == "txtProjectFolder")
                {
                    SetAllSavePath();
                }
            }
        }

        private void btnServiceModelBrowser_Click(object sender, EventArgs e)
        {
            SetSavePath(txtServiceModelFolder);

        }

        private void SetAllSavePath()
        {
            string projectName = txtProjectNameSpace.Text;
            txtEntityFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".Entity").Replace(@"\\", @"\");
            txtMapFolder.Text = (txtEntityFolder.Text + @"\Map").Replace(@"\\", @"\");
            txtIRespositoryFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".Repository").Replace(@"\\", @"\");
            txtRespositoryFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".IBatisRepository").Replace(@"\\", @"\");
            txtIServiceFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".Service.Contracts").Replace(@"\\", @"\");
            txtServiceFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".Service").Replace(@"\\", @"\");
            txtServiceModelFolder.Text = (txtProjectFolder.Text + @"\" + projectName + ".Service.Model").Replace(@"\\", @"\");

        }

        #endregion


        private void button1_Click(object sender, EventArgs e)
        {
            SetAttr();
            XmlHelper.Update(info);
        }

        private void txtProjectNameSpace_TextChanged(object sender, EventArgs e)
        {
            string namePace = txtProjectNameSpace.Text;
            txtEntityNameSpace.Text = namePace + ".Entity";
            txtIRespositoryNameSpace.Text = namePace + ".Respository";
            txtRespositoryNameSpace.Text = namePace + ".IBatisRepository";
            txtIServiceNameSpace.Text = namePace + ".Service.Contracts";
            txtServiceNameSpace.Text = namePace + ".Service";
            txtServiceModelNameSpace.Text = namePace + ".Service.Model";

        }

    }
}
